<template>
	<view class="content">
        <uni-password :show="true" digits="6" @confirm="onConfirm">
        	请输入支付密码
        </uni-password>
		<view class="g-frame-mask" v-if="showPopup"></view>
		<view class="g-frame" v-if="showPopup">
			<view class="m-frame-header">
			    <view class="u-frame-title">提示</view>
			    <image class="u-frame-close" src="../../static/icons/ydt/close.png" mode="widthFix" @tap="togglePopup"></image>
			</view>
			<view class="u-frame-cont" v-if="pageType =='transform' && sendTransData.state == 0">本次互转{{price}}粮仓，请确认</view>
			<view class="u-frame-cont" v-if="pageType =='allowanceturn'">本次转出{{price}}津贴，请确认</view>
			<view class="u-frame-cont" v-if="pageType =='takeAllowance'">本次退取{{price}}津贴，请确认</view>
			<view class="u-frame-cont" v-if="pageType =='hlb_wit'">本次退取{{price}}恒朗宝，请确认</view>
			<view class="u-frame-cont" v-if="pageType == 'kdturn' || pageType == 'takecash'">本次退取{{price}}粮仓，请确认</view>
			<view class="u-frame-cont" v-if="pageType =='transform' && sendTransData.state == 1">本次互转{{price}}恒朗宝，请确认</view>
			<view class="m-frame-footer">
			    <view class="u-reset" @tap="togglePopup">取消</view>
			    <view class="u-confirm" @tap="toConfirm">确定</view>
			</view>
		</view>
		<view class="m-full-loading" v-if="showloading">
			<view class="m-loading">
				<image src="../../static/images/loading.gif" mode="widthFix"></image>
			    <text>加载中</text>
			</view>
		</view>
	</view>
</template>

<script>
	
	import henglang from '@/common/common.js'
	import md5 from '@/common/md5.js'
	export default {
		data() {
			return {
				//提现
				pageType:'',
				sendData:{
					amount:'',
					method:'',
					account:'',
					paypwd: '',
					code:'',
					state:''
				},
				//蝌蚪转粮仓
				sendKdData:{
					kdamount:''
				},
				//粮仓互转
				sendTransData:{
					amount:'',
					user_id:'',
					state:0
				},
				showloading: false,
				showPopup: false,
				price: '',
				password: ''
			}
		},
		methods: {
			togglePopup() {
			    this.showPopup = !this.showPopup;
			},
			onConfirm(e) {
				this.showPopup = true;
				this.password = md5(e);
			},
			toConfirm() {
				let that = this;
				that.showPopup = false;
				that.showloading = true;
				switch (that.pageType){
					case 'kdturn':
						setTimeout(() => {
							that.postKdData()
						}, 500)
						break;
					case 'transform':
						setTimeout(() => {
							that.postTransData()
						}, 500)
						break;
					case 'hlb_wit':
					case 'takeAllowance':
					case 'takecash':
						setTimeout(() => {
							that.postData()
						}, 500)
						break;
					case 'allowanceturn':
						setTimeout(() => {
							that.postKdData()
						}, 500)
						break;
					default:
						break;
				}
			},
			//提交提现数据
			postData() {
				let that = this
				that.sendData.paypwd = that.password;
				henglang.post('withdraw/applyWithdraw',that.sendData,true,function(res){
					that.showloading = false;
					if(res.data.code == 0){
						henglang.showToast(res.data.msg,500);
						setTimeout(() => {
							uni.redirectTo({
								url:'/pages/user/hl_takedeital'
							})
						}, 500)
					}else if(res.data.code == -2){
						henglang.showToast(res.data.msg);
						setTimeout(() => {
							uni.navigateBack()
						}, 3000)
					}else{
						henglang.showToast(res.data.msg);
					}
				});
			},
			//提交蝌蚪转粮仓数据
			postKdData(password) {
				let that = this
				let type = that.pageType == 'allowanceturn'?3:2
				henglang.post('transfer/kedouToMoney',{amount:that.sendKdData.kdamount,paypwd:that.password,type:type},true,function(res){
					that.showloading = false;
					if(res.data.code == 0){
						henglang.showToast(res.data.msg,500);
						setTimeout(() => {
							uni.redirectTo({
								url:'/pages/common/bill?type=1' + '&tar=1'
							})
						}, 500)
					}else{
						henglang.showToast(res.data.msg);
					}
				},function(res){
					henglang.showToast('请求失败');
					return;
				});
			},
			//提交粮仓互转数据
			postTransData(password) {
				let that = this
				that.sendTransData.paypwd = that.password
				henglang.post('transfer/betweenMember',that.sendTransData,true,function(res){
					that.showloading = false;
					if(res.data.code == 0){
						henglang.showToast(res.data.msg,500);
						setTimeout(() => {
							that.backSetData('true',res.data.data.in_money)
						}, 500)
					}else{
						henglang.showToast(res.data.msg);
					}
				},function(res){
					henglang.showToast('请求失败');
					return;
				});
			},
			//返回上一页带参数
			backSetData(val,val2) {
				var pages = getCurrentPages();
				var currPage = pages[pages.length - 1]; //当前页面
				var prevPage = pages[pages.length - 2]; //上一个页面
				//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
				prevPage.setData({
				    isTransOk:val,
					in_money:val2
				})
				uni.navigateBack();
			}
		},
		onLoad(option) {
			this.pageType = option.pageType
			this.price = option.price
			this.sendData = {
				amount:option.amount,
				method:option.method,
				account:option.account,
				code:option.code,
				state:option.state
			}
			this.sendKdData = {
				kdamount:option.kdamount
			}
			this.sendTransData = {
				amount:Number(option.trans_amount),
				user_id:option.user_id,
				state:option.state
			}
		}
	}
</script>
<style>
	.content {
		text-align: center;
		height: 400upx;
	}
	.m-full-loading{
		background-color: transparent;
	}
	.m-loading{
		z-index: 21;
	}
	.g-frame {
	    width: 581upx;
	    background: #fff;
	    border-radius: 20upx;
	    position: fixed;
	    top: 50%;
	    overflow: hidden;
	    left: 50%;
		margin-top: -215upx;
		margin-left: -290upx;
		z-index: 12;
	}
	
	.g-frame-mask {
	    background: #000;
	    opacity: .5;
	    height: 100vh;
	    width: 100%;
	    position: fixed;
	    top: 0upx;
		z-index: 11;
	}
	
	.m-frame-header {
	    height: 110upx;
	    width: 100%;
	    background: #d71311;
	    border-top-right-radius: 20upx;
	    border-top-left-radius: 20upx;
		position: relative;
		margin-bottom: 35upx;
	}
	.u-frame-title {
	    width: 100%;
	    text-align: center;
	    color: #fff;
	    line-height: 110upx;
	    font-size: 32upx;
	}
	
	.u-frame-close {
	    width: 42upx;
	    height: 42upx;
	    background-size: 100%;
	    position: absolute;
	    top: 50%;
		margin-top: -21upx;
	    right: 32upx;
	}
	.u-frame-cont{
		text-align: center;
		font-size: 36upx;
		padding-bottom: 35upx;
		line-height: 60upx;
		width: 500upx;
		margin: 0 auto;
	}
	.m-frame-footer {
	    height: 100upx;
	    width: 100%;
	    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
		display: flex;
		align-items: center;
		margin-top: 20upx;
	}
	
	.m-frame-footer view {
		flex: 1;
	    line-height: 100upx;
	    text-align: center;
	    font-size: 32upx;
	}
	
	.u-reset {
	    color: #ff0000;
	    background: #fff;
	}
	
	.u-confirm {
	    background: #d71311;
	    color: #fff;
	    border-bottom-right-radius: 16upx;
	}
	
</style>
